<!DOCTYPE html "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">


<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<div class="wrapper wrapper-content ">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form class="form-horizontal m-t" id="signupForm">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">产品名称：</label>
                            <div class="col-sm-10">
                                <input id="productName" name="productName" class="form-control" type="text" maxlength="6" placeholder=""  th:value="${productInfo.productName}" >
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">产品副标题(描述)：</label>
                            <div class="col-sm-10">
                                <input id="productDesc" name="productDesc" th:value="${productInfo.productDesc}"
                                       class="form-control" type="text"  maxlength="10" >
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">上传图片：</label>
                            <div class="col-sm-10">
                                <input @change="fileUpload($event)" accept="image/*" type="file">
                            </div>
                        </div>

                        <div class="form-group" id="cardDiv2" v-if="logoimgDataURL">
                            <div class="col-sm-8 col-sm-offset-3">
                                <img :src="logoimgDataURL" alt="产品图片" class="img-responsive img-thumbnail"
                                     style="height: 80px;width: 120px">
                                <input type="hidden" name="logoImg" id="logoImg" th:value="${productInfo.logoImg}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">所属分类：</label>
                            <div class="col-sm-10">
                                <select name="classId" id="classId" >
                                    <option th:each="productClass,appStat:${productClasses}" th:value="${productClass.classId}" th:text="${productClass.className}" th:selected="${productClass.classId == productInfo.classId}"></option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">最低额度：</label>
                            <div class="col-sm-4">
                                <input type="text" id="minLimit" name="minLimit" th:value="${productInfo.minLimit}"
                                       data-placeholder="最低额度" class="form-control" onkeyup="value=value.replace(/[^\d\.]/g,'')"  >
                            </div>
                            <label class="col-sm-2 control-label">最高额度：</label>
                            <div class="col-sm-4">
                                <input type="text" id="maxLimit" name="maxLimit" th:value="${productInfo.maxLimit}"
                                       data-placeholder="最高额度" class="form-control" onkeyup="value=value.replace(/[^\d\.]/g,'')" >
                            </div>
                        </div>


                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态:</label>
                            <div class="col-sm-10">
                                <label class="radio-inline">
                                    <input type="radio" name="status" value="1"   th:field="*{productInfo.status}" />上架
                                </label>
                                <label class="radio-inline">
                                    <input type="radio" name="status" value="0"   th:field="*{productInfo.status}" />下架
                                </label>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">是否置顶:</label>
                            <div class="col-sm-10">
                                <label class="radio-inline">
                                    <input type="radio" name="isTop" value="1"   th:field="*{productInfo.isTop}" />是
                                </label>
                                <label class="radio-inline">
                                    <input type="radio" name="isTop" value="0"   th:field="*{productInfo.isTop}" />否
                                </label>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">链接地址：</label>
                            <div class="col-sm-10">
                                <input id="linkUrl" name="linkUrl" th:value="${productInfo.linkUrl}" class="form-control"
                                       type="text" maxlength="180" placeholder="http://..." >
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">排序号：</label>
                            <div class="col-sm-10">
                                <input id="orderNum" name="orderNum" class="form-control"
                                       type="text" th:value="${productInfo.orderNum}">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">备注：</label>
                            <div class="col-sm-10">
                                <textarea id="remark" name="remark" th:text="${productInfo.remark}"
                                          class="form-control" placeholder="产品详情说明..."  maxlength="180" ></textarea>
                            </div>
                        </div>

                        <div class="text-right form-group">
                            <button type="submit" class="btn btn-primary">复制</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:include="include::footer"></div>
<script th:inline="javascript">
    $().ready(function () {
        validateRule();

    });

    $("#minLimit").on("input", function (evt) {
        var self = $(this);
        self.val(self.val().replace(/[^0-9\.]/g, ''));
        if ((evt.which != 46 || self.val().indexOf('.') != -1) && (evt.which < 48 || evt.which > 57)) {
            evt.preventDefault();
        }
    });

    $("#maxLimit").on("input", function (evt) {
        var self = $(this);
        self.val(self.val().replace(/[^0-9\.]/g, ''));
        if ((evt.which != 46 || self.val().indexOf('.') != -1) && (evt.which < 48 || evt.which > 57)) {
            evt.preventDefault();
        }
    });

    // 上传图片
    const vue = new Vue({
        el: '#signupForm',
        data: {
            reqData: {},
            logoimgDataURL: [[${productInfo.logoImg}]],
        },
        methods: {
            fileUpload: function (evt) {
                const files = evt.target.files;
                if (files.length == 0) {
                    return;
                }

                const file = files[0];
                if (!/image\/\w+/.test(file.type)) {
                    parent.layer.alert("请选择图像类型的文件！");
                    return;
                }

                let vm = this;

                const fileType = '.' + file.name.toLowerCase().split('.').pop();

                const reader = new FileReader();
                reader.onload = (function (file) {
                    return function (e) {
                        const result = e.target.result;
                        vm.logoimgDataURL = result;
                        const base64Str = result.split(",")[1];
                        //上传地址动态获取
                        var uploadUrl = "/api/fileUpload";
                        $.post(uploadUrl, {fileType, base64Str}, function (resp) {
                            if (!!resp) {
                                const base64 = new Base64();
                                const respData = JSON.parse(base64.decode(resp));
                                if (respData.code === 0) {
                                    $("#logoImg").val(respData.data.imgPath);
                                }
                            }
                        });
                    }
                })(file);
                reader.readAsDataURL(file);
            }
        }

    });

    $.validator.setDefaults({
        submitHandler: function () {
            save();
        }
    });


    function save() {
        //var content_sn = $("#content_sn").summernote('code');
        //$("#productDetail").val(content_sn);
        $.ajax({
            cache: true,
            type: "POST",
            url: "/app/product/copyInsert",
            data: $('#signupForm').serialize(), // 你的formid
            async: false,
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    parent.reLoad();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#signupForm").validate({
            rules: {
                productName: {
                    required: true
                },
                productDesc:{
                    required: true
                },
                logoImg:{
                    required: true
                },
                classId:{
                    required: true
                },
                minLimit:{
                    required: true
                },
                maxLimit:{
                    required: true
                },
                linkUrl:{
                    required: true
                },
                orderNum:{
                    required: true
                }
            },
            messages: {
                bannerName: {
                    required: icon + "必填"
                },
                productDesc:{
                    required: icon + "必填"
                },
                logoImg: {
                    required: icon + "必填"
                },
                classId: {
                    required: icon + "必填"
                },
                minLimit: {
                    required: icon + "必填"
                },
                maxLimit: {
                    required: icon + "必填"
                },
                linkUrl:{
                    required: icon + "必填"
                },
                orderNum:{
                    required: icon + "必填"
                }
            }
        })
    }

</script>
</body>
</html>